﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickPasteItLib;
using System.IO;

namespace QuickPasteItCmd
{
    class Program
    {
        private const string ERROR_MESSAGE       = "ERROR: Invalid file specified.";
        private const string PASTE_ERROR_MESSAGE = "ERROR: A problem occurred while communicating with the pastebin.";

        private const string HELP_MESSAGE = "\n     Usage: qpi.exe FILE [PASTEBIN]\n\n" +
                                              " Pastebins: " + QpiConfig.DEFAULT_PASTEBIN + "\n";

        /// <summary>
        /// Secondary entry point
        /// </summary>
        public void Run(string[] args)
        {
            try
            {
                // args[0] will always be the filename or "?" or "help"
                // args[1] will sometimes be a pastebin name matching pasteBinMap
                if (args.Length != 1 && args.Length != 2)
                    throw new ArgumentException("Invalid arguments supplied.");

                if (args[0].ToLower() == "help" || args[0] == "?")
                {
                    ShowHelp();
                    Environment.Exit(0);
                }

                FileInfo givenFile = new FileInfo(args[0]);
                Encoding fileEnc = Encoding.UTF8;
                QpiConfig qpiConfig = new QpiConfig();

                if (!givenFile.Exists || ((fileEnc = FileTools.DetectFileEncoding(givenFile)) == null && FileTools.FileHasNullBytes(givenFile)))
                {
                    Console.WriteLine(ERROR_MESSAGE);
                }
                else
                {
                    // Get the pastebin to use -- fall back to user's preferred if none given
                    IPasteBin pastebin = args.Length == 2 ? QpiConfig.GetPasteBin(args[1]) : QpiConfig.GetPasteBin(qpiConfig.PreferredPastebin);
                    
                    // Read file into string
                    string textToPaste = new StreamReader(givenFile.FullName, fileEnc).ReadToEnd();

                    var submitResult = pastebin.SubmitPaste(textToPaste, FileTools.GetPasteLangFromExtension(givenFile.Extension), qpiConfig.Author);
                    if (submitResult != "")
                        Console.WriteLine("\nPastebinned at: {0}", submitResult);
                    else
                        Console.WriteLine(PASTE_ERROR_MESSAGE);
                }
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
                ShowHelp();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Oops! That wasn't supposed to happen...\n");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        /// <summary>
        /// Prints the help message for the program.
        /// </summary>
        private void ShowHelp()
        {
            Console.Write(HELP_MESSAGE);
            foreach (string pastebin in QpiConfig.pasteBinMap.Keys)
                if (pastebin != QpiConfig.DEFAULT_PASTEBIN)
                    Console.WriteLine(new String(' ', 12) + pastebin);
        }

        /// <summary>
        /// Main entry point
        /// </summary>
        static void Main(string[] args)
        {
            new Program().Run(args);
        }
    }
}
